Audio Element Resampling

I wanted to test if a PCM audio file (wav) given to an HTML Audio Element gets resampled when played back. If so, to what sampling frequency. I had a hunch that it would get resampled to the underlying platforms default sampling frequency, but I devised a method to test this.

OSX has a mechanism to change the underlying platform's sampling rate. Using this as well as Jupyter's IPython.display.Audio object which allows a numpy array to be converted to a ObjectURL and passed to an audio tag. I could quickly generate and play various tones at various sampling rates.

Looking at the output on an oscilloscope, it's easy to see how and when audio gets resampled.

In [1]:
from IPython.display import Audio
import numpy as np
In [2]:
# Sampling Rate of 44100 Hz (standard for most audio cards) and 200Hz Tone
# This should show a wave at 200Hz
samplingrate = 44100
t = np.linspace(0,5,samplingrate*5)
data = np.sin(2*np.pi*200*t)
Audio(data,rate=samplingrate)
Out[2]:
In [3]:
# Sampling Rate of 44100 Hz (standard for most audio cards) and 20kHz Tone
# This should show a wave at 20kHz
samplingrate = 44100
t = np.linspace(0,5,samplingrate*5)
data = np.sin(2*np.pi*20000*t)
Audio(data,rate=samplingrate)
Out[3]:
In [4]:
# Sampling Rate of 44100 Hz (standard for most audio cards) and 30kHz Tone.
# This should show an aliased wave at ~14kHz
samplingrate = 44100
t = np.linspace(0,5,samplingrate*5)
data = np.sin(2*np.pi*30000*t)
Audio(data,rate=samplingrate)
Out[4]:
In [5]:
# Sampling Rate of 96000 Hz (set this in the Audio MIDI Setup on a Mac) and 30kHz Tone.
# This should show a wave at 30kHz
samplingrate = 96000
t = np.linspace(0,5,samplingrate*5)
data = np.sin(2*np.pi*30000*t)
Audio(data,rate=samplingrate)
Out[5]:
In [6]:
# Sampling Rate of 200000 Hz (waay higher than what most laptops can do) and 30kHz Tone.
# This should show a wave at 30kHz
samplingrate = 200000
t = np.linspace(0,5,samplingrate*5)
data = np.sin(2*np.pi*30000*t)
Audio(data,rate=samplingrate)
Out[6]:
In [7]:
# Sampling Rate of 200000 Hz (waay higher than what most laptops can do) and 47kHz Tone.
# This should show a wave at 47kHz
samplingrate = 200000
t = np.linspace(0,5,samplingrate*5)
data = np.sin(2*np.pi*49000*t)
Audio(data,rate=samplingrate)
Out[7]:
In [8]:
# Sampling Rate of 200000 Hz (waay higher than what most laptops can do) and 55kHz Tone.
# There should be no output as 55kHz is higher than the max output sampling rate of my Macbook (48kHz).
samplingrate = 200000
t = np.linspace(0,5,samplingrate*5)
data = np.sin(2*np.pi*55000*t)
Audio(data,rate=samplingrate)
Out[8]: